<template>
  <div class="user">
    <basic-container>
      <avue-crud
        :option="option"
        ref="crud"
        v-model="form"
        :page="page"
        @on-load="getList"
        @size-change="sizeChange"
        @current-change="currentChange"
        :table-loading="listLoading"
        @search-change="handleFilter"
        @refresh-change="handleRefreshChange"
        :before-open="handleOpenBefore"
        @search-reset="handleRefreshChange"
        @row-update="update"
        @row-save="create"
        @row-del="del"
        :data="list"
      >
        <template slot="menu" slot-scope="scope">
          <el-button
            class="filter-item"
            @click="handleUpdate(scope.row)"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.status === 2"
            >下架
          </el-button>
          <el-button
            class="filter-item"
            @click="handleUpdateDomain(scope.row)"
            type="text"
            icon="el-icon-folder-checked"
            v-if="scope.row.status === 52"
            >到期过户
          </el-button>
          <el-button
            class="filter-item"
            @click="handleUpdateDomain(scope.row)"
            type="text"
            icon="el-icon-folder-checked"
            v-if="scope.row.status === 51"
            >回购过户
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
    <el-dialog
      title="域名过户"
      class="payMethod"
      :visible.sync="dialogDomain"
      width="40%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="域名">
          <el-input v-model="domainInfo.domain" readonly></el-input>
        </el-form-item>
        <div v-if="domainInfo.status === 51">
          <el-form-item label="卖家ID">
            <el-input v-model="domainInfo.user_id" readonly></el-input>
          </el-form-item>
          <el-form-item label="卖家姓名">
            <el-input v-model="domainInfo.name" readonly></el-input>
          </el-form-item>
        </div>
        <div v-else>
          <el-form-item label="买家ID">
            <el-input v-model="domainInfo.sell_id" readonly></el-input>
          </el-form-item>
          <el-form-item label="买家姓名">
            <el-input v-model="domainInfo.sell_name" readonly></el-input>
          </el-form-item>
        </div>
        
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogDomain = false">取 消</el-button>
        <el-button type="primary" @click="handleDialogDomain">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, putObj, delObj, updateDomain } from "@/api/admin/orderList";
import { getObj } from "@/api/admin/user";
import { tableOption } from "@/const/crud/admin/orderList";
import { mapGetters } from "vuex";

export default {
  name: "table_user",
  data() {
    return {
      option: tableOption,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条,
      },
      list: [],
      listLoading: true,
      dialogDomain: false,
      param: {},
      form: {},
      domainInfo: {}
    };
  },
  computed: {
    ...mapGetters(["permissions"])
  },
  created() {
  },
  methods: {
    getList(page, params) {
      this.listLoading = true;
      fetchList(
        Object.assign(
          {
            page: page.currentPage,
            size: page.pageSize
          },
          this.param
        )
      ).then(response => {
        this.list = response.data.data.data;
        this.page.total = response.data.data.count || '0';
        this.listLoading = false;
      });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList(this.page);
    },
    currentChange(current) {
      this.page.currentPage = current;
      this.getList(this.page);
    },
    handleFilter(param, done) {
      this.page.currentPage = 1;
      this.param = param
      if(this.param.create_time){
        this.param.start = this.param.create_time[0]
        this.param.end = this.param.create_time[1]
        delete this.param.create_time
      }
      this.getList(this.page, param);
      done();
    },
    handleRefreshChange() {
      this.param = {}
      this.getList(this.page);
    },
    handleOpenBefore(show, type) {
      show();
    },
    goWhios(row){
      console.log(row)
      // https://www.ggcx.com/main/integrated?domain=fcd.com
    },
    changeFlag(status, id) {
      let num;
      if (status === 0) {
        num = 1;
      } else {
        num = 0;
      }
      updateUser({ status: num, id: id }).then(res => {
        this.getList(this.page);
        this.$notify.success("操作成功");
      });
    },
    handleUpdate(row) {
      putObj({id: row.id, status: 8}).then(res=>{
        this.getList(this.page);
        this.$notify.success("操作成功");
      })
    },
    handleUpdateDomain(row){
      this.domainInfo = row
      let userId = 0
      if(this.domainInfo.status === 51){
        userId = this.domainInfo.user_id
      }else{
        userId = this.domainInfo.sell_id
      }
      getObj({id: userId}).then(res=>{
        if(res.data.data.user_no){
          if(this.domainInfo.status === 51){
            this.domainInfo.user_id = res.data.data.user_no
          }else{
            this.domainInfo.sell_id = res.data.data.user_no
          }
        }
      })
      this.dialogDomain = true
    },
    handleDialogDomain(){
      updateDomain({id: this.domainInfo.id})
        .then(() => {
          this.$notify.success("过户成功");
          this.dialogDomain = false
          this.getList(this.page);
        })
    },
    create(row, done, loading) {
      putObj(row)
        .then(() => {
          this.getList(this.page);
          done();
          this.$notify.success("创建成功");
        })
        .catch(() => {
          loading();
        });
    },
    update(row, index, done, loading) {
      putObj(this.form)
        .then(() => {
          this.getList(this.page);
          done();
          this.$notify.success("修改成功");
        })
        .catch(() => {
          loading();
        });
    },
    del(row){
      this.$confirm(
        "此操作将永久删除, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        delObj(row.id)
          .then(() => {
            this.getList(this.page);
            this.$notify.success("操作成功");
          })
          .catch(() => {
            this.$notify.error("操作失败");
          });
      });
    },
  }
};
</script>
